<template>
  <p-card
    :loading="cardLoading"
    :bordered="false"
    style="height: 100%;"
  >
    <p-spin :spinning="loading">
      <!--<p-input-search @search="handleSearch" style="width:100%;margin-top: 10px" placeholder="输入机构名称查询..." allowClear enterButton />-->

      <p-tree
        show-line
        check-strictly
        :expanded-keys.sync="expandedKeys"
        :selected-keys="selectedKeys"
        :dropdown-style="{maxHeight:'200px',overflow:'auto'}"
        :tree-data="treeDataSource"
        @select="handleTreeSelect"
      />
    </p-spin>
  </p-card>
</template>

<script>

// import { loadTreeCategoryData } from '@/api/api'
export default {
  name: 'FxArticleListLeft',
  props: ['value'],
  data () {
    return {
      cardLoading: true,
      loading: false,
      treeDataSource: [],
      selectedKeys: [],
      expandedKeys: []
    }
  },
  created () {
    this.queryTreeData()
  },
  methods: {

    queryTreeData (keyword) {
      console.log('类型关键词：', keyword)
      // this.commonRequestThen(loadTreeCategoryData({
      //     code: 'B03'
      // }))
    },

    /* handleSearch(value) {
            if (value) {
              this.commonRequestThen(searchByKeywords({ keyWord: value }))
            } else {
              this.queryTreeData()
            }
          }, */

    handleTreeSelect (selectedKeys, event) {
      if (selectedKeys.length > 0 && this.selectedKeys[0] !== selectedKeys[0]) {
        this.selectedKeys = [selectedKeys[0]]
        const articleType = event.node.dataRef.id
        this.emitInput(articleType)
      }
    },

    emitInput (articleType) {
      this.$emit('input', articleType)
    },

    commonRequestThen (promise) {
      this.loading = true
      promise.then(res => {
        if (res.success) {
          this.treeDataSource = res.data
          // update-begin- --- author:wangshuai ------ date:20200102 ---- for:去除默认选中第一条数据、默认展开所有第一级
          // 默认选中第一条数据、默认展开所有第一级
          // if (res.data.length > 0) {
          //   this.expandedKeys = []
          //   res.data.forEach((item, index) => {
          //     if (index === 0) {
          //       this.selectedKeys = [item.id]
          //       this.emitInput(item.orgCode)
          //     }
          //     this.expandedKeys.push(item.id)
          //   })
          // }
          // update-end- --- author:wangshuai ------ date:20200102 ---- for:去除默认选中第一条数据、默认展开所有第一级
        } else {
          this.$message.warn(res.msg)
          console.error('分类查询失败:', res)
        }
      }).finally(() => {
        this.loading = false
        this.cardLoading = false
      })
    }

  }
}

</script>
